package cc.lj.internal.coupons.interfaces.dto.convert;

import cc.lj.internal.api.coupons.dto.TicketBatchApiDTO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.interfaces.dto.TicketBatchDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;

/**
 * 电子券批次表DO与DTO转换
 * @author liangxifeng
 * @date 2021-12-13
 */
@Mapper
public interface TicketBatchDTOConvertDO {
    TicketBatchDTOConvertDO INSTANCE = Mappers.getMapper(TicketBatchDTOConvertDO.class);

    //dto to do
    TicketBatchDO convert(TicketBatchDTO ticketBatchDTO);

    /**
     * DO转DTO时，商户和商城分摊比需要格式化为：*100
     * @param ticketBatchDO
     * @return
     */
    @Mapping(source = "batchMerchantShare",target = "batchMerchantShare",qualifiedByName = "merchantShareMutiply")
    @Mapping(source = "batchStoreShare",target = "batchStoreShare",qualifiedByName = "martShareMutiply")
    //do to dto
    TicketBatchDTO convert(TicketBatchDO ticketBatchDO);
    /**
     * DO转DTO时，商户和商城分摊比需要格式化为：*100
     * @param ticketBatchDO
     * @return
     */
    @Mapping(source = "batchMerchantShare",target = "batchMerchantShare",qualifiedByName = "merchantShareMutiply")
    @Mapping(source = "batchStoreShare",target = "batchStoreShare",qualifiedByName = "martShareMutiply")
    //do to dto
    TicketBatchApiDTO convertToApiDTO(TicketBatchDO ticketBatchDO);
    /**
     * 商户分摊比例 *100
     * @param batchMerchantShare 商户分摊比
     * @return 商户分摊比*100
     */
    @Named("merchantShareMutiply")
    default BigDecimal merchantShareMutiply(BigDecimal batchMerchantShare) {
        if(batchMerchantShare != null) {
            return batchMerchantShare.multiply(new BigDecimal(100));
        } else {
            return null;
        }
    }
    /**
     * 卖场分摊比例 *100
     * @param batchStoreShare
     * @return
     */
    @Named("martShareMutiply")
    default BigDecimal martShareMutiply(BigDecimal batchStoreShare) {
        if(batchStoreShare != null) {
            return batchStoreShare.multiply(new BigDecimal(100));
        } else {
            return null;
        }
    }
}
